const app = getApp()
var content = ""

Page({
    data: {},
    onLoad(options) {
        this.setData(options)
    },

    onShow() {
        this.setData({
            goods: app.tmpGoods
        })
        if (this.data.check) {
            wx.showLoading({
                title: '加载中...',
            })
            app.http.get("member/comment-view", {
                data: { id: this.data.id, goods_id: this.data.goods.goods_id },
                showMsg: true
            }).then(res => {
                res.data.stars = res.data.star
                this.setData(res.data)
                wx.hideLoading()
            })
        }
    },
    onInput(evt) {
        content = evt.detail.value
    },

    clickStar(evt) {
        if (this.data.check) return
        this.setData({
            stars: evt.currentTarget.dataset.star
        })
    },
    submit() {
        if (!content.trim()) {
            app.wxToast.warn("评论内容不能为空")
            return
        }
        this.setData({ loading: true })
        app.http.post("member/comment", {
            data: {
                id: this.data.id,
                goods_id: this.data.goods.goods_id,
                content: content,
                star: this.data.stars
            },
        }).then(res => {
            wx.navigateBack()
        })
    },
    onUnload() {
        app.tmpGoods = null
        console.log('ss')
    },
})